package ialsynth2012;

/**
 *
 * @author Linus Wendel
 */
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class SynthGui extends JPanel implements ActionListener {

    private String[] waveStrings = {"Preset 1", "Preset 2", "Preset 3"};
    private JSlider adsrSlider[] = new JSlider[4];
    private JSlider volumeSlider;
    private JLayeredPane layeredPane;
    private JComboBox waveList;
    private JToggleButton save;
    private JTextField presetName;
    private static final int WIDTH = 480;
    private static final int HEIGHT = 800;
    private static final int ADSR_MIN = 0;
    private static final int ADSR_MAX = 100;
    private static final int ADSR_INIT = 80;
    private JButton white[] = new JButton[8];
    private JButton black[] = new JButton[8];
    private int space = 50;
    private SynthInstrument synthInstrument;

    public SynthGui() {
        setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
        //Create and set up the layered pane.
        synthInstrument = new SynthInstrument();
        layeredPane = new JLayeredPane();
        layeredPane.setPreferredSize(new Dimension(0, 300));
        layeredPane.setBorder(BorderFactory.createTitledBorder(
                "Keys"));

        //Make white keys
        int widthW = 58;
        int heightW = 240;
        
        for (int i = 0; i < white.length; i++) {
            white[i] = new JButton();
            white[i].setBackground(Color.WHITE);
            white[i].setLocation(i * widthW, 70);
            white[i].setSize(widthW, heightW);
            layeredPane.add(white[i], 0, -1);
        }
        
        white[0].addActionListener(new ActionListener() {
 
            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                System.out.println("You clicked C");
                synthInstrument.noteOn(36);
                synthInstrument.noteOff(36);
            }
        }); 
        
        white[1].addActionListener(new ActionListener() {
 
            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                System.out.println("You clicked D");
                synthInstrument.noteOn(38);
                synthInstrument.noteOff(38);
            }
        });  
        
        white[2].addActionListener(new ActionListener() {
 
            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                System.out.println("You clicked E");
                synthInstrument.noteOn(40);
                synthInstrument.noteOff(40);
            }
        }); 
        white[3].addActionListener(new ActionListener() {
 
            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                System.out.println("You clicked F");
                synthInstrument.noteOn(41);
                synthInstrument.noteOff(41);
            }
        }); 
        
        white[4].addActionListener(new ActionListener() {
 
            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                System.out.println("You clicked G");
                synthInstrument.noteOn(43);
                synthInstrument.noteOff(43);
                System.out.println(e.getActionCommand());
            }
        }); 
        
        white[5].addActionListener(new ActionListener() {
 
            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                System.out.println("You clicked A");
                synthInstrument.noteOn(45);
                synthInstrument.noteOff(45);
            }
        }); 
        
        white[6].addActionListener(new ActionListener() {
 
            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                System.out.println("You clicked B");
                synthInstrument.noteOn(47);
                synthInstrument.noteOff(47);
            }
        }); 
        
        white[7].addActionListener(new ActionListener() {
 
            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                System.out.println("You clicked C");
                synthInstrument.noteOn(48);
                synthInstrument.noteOff(48);
            }
        }); 
                
        //Make black keys    
        int widthB = 46;
        int heightB = 140;

        for (int i = 0; i < black.length; i++) {
            int j = i % 7;
            if (j == 2 || j == 6) {
                continue;
            }
            black[i] = new JButton();
            black[i].setBackground(Color.BLACK);
            black[i].setLocation(i * (widthW) + (widthB * 3 / 4), 70);
            black[i].setSize(widthB, heightB);
            layeredPane.add(black[i], 1, -1);
        }
        black[0].addActionListener(new ActionListener() {
 
            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                System.out.println("You clicked 1");
                synthInstrument.noteOn(37);
                synthInstrument.noteOff(37);
            }
        }); 
        
        black[1].addActionListener(new ActionListener() {
 
            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                System.out.println("You clicked 2");
                synthInstrument.noteOn(39);
                synthInstrument.noteOff(39);
            }
        }); 
        black[3].addActionListener(new ActionListener() {
 
            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                System.out.println("You clicked 3");
                synthInstrument.noteOn(42);
                synthInstrument.noteOff(42);
            }
        }); 
        black[4].addActionListener(new ActionListener() {
 
            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                System.out.println("You clicked 4");
                synthInstrument.noteOn(44);
                synthInstrument.noteOff(44);
            }
        }); 
        black[5].addActionListener(new ActionListener() {
 
            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                System.out.println("You clicked 5");
                synthInstrument.noteOn(46);
                synthInstrument.noteOff(46);
            }
        }); 
        black[7].addActionListener(new ActionListener() {
 
            public void actionPerformed(ActionEvent e)
            {
                //Execute when button is pressed
                System.out.println("You clicked 6");
                synthInstrument.noteOn(49);
                synthInstrument.noteOff(49);
            }
        }); 

        
        

        //Add control pane and layered pane to this JPanel.
        add(Box.createRigidArea(new Dimension(0, 10)));
        add(createControlPanel());
        add(Box.createRigidArea(new Dimension(0, 10)));
        add(layeredPane);
        //Add Volume pane
        add(Box.createRigidArea(new Dimension(0, 10)));
        add(createVolumePanel());
        add(Box.createRigidArea(new Dimension(0, 10)));
        add(layeredPane);
        //Add Adsr pane
        add(Box.createRigidArea(new Dimension(0, 10)));
        add(createAdsrPanel());
        add(Box.createRigidArea(new Dimension(0, 10)));
        add(layeredPane);
    }

    //Create the control pane for the top of the frame.
    private JPanel createControlPanel() {
        //Save button
        save = new JToggleButton("Load");
        //presetName = new JTextField(20);

        //Waveform list
        waveList = new JComboBox(waveStrings);
        waveList.addActionListener(this);

        JPanel controls = new JPanel();
        controls.add(waveList);
        //controls.add(presetName);
        controls.add(save);
        controls.setBorder(BorderFactory.createTitledBorder(
                "Menu"));
        //controls.setBackground(Color.gray);
        return controls;
    }

    private JPanel createAdsrPanel() {
        JPanel adsr = new JPanel();
        for (int i = 0; i < adsrSlider.length; i++) {
            adsrSlider[i] = new JSlider(JSlider.VERTICAL, ADSR_MIN, ADSR_MAX,
                    ADSR_INIT);
            adsrSlider[i].setMajorTickSpacing(20);
            adsrSlider[i].setMinorTickSpacing(5);
            adsrSlider[i].setPaintTicks(true);
            //adsrSlider[i].setBackground(Color.red);
            adsrSlider[i].setBounds(space, 130, 40, 200);
            //indSlider[i].addChangeListener(new SliderListener(i));   
            adsr.add(adsrSlider[i]);
            space += 50;
        }


        adsrSlider[0].addChangeListener(new ChangeListener() {
            // This method is called whenever the slider's value is changed

            public void stateChanged(ChangeEvent evt) {

                if (!adsrSlider[0].getValueIsAdjusting()) {
                    // Get new value
                    int value = adsrSlider[0].getValue();
                    float floatValue = (float)value;
                    //System.out.println("1 " + value);
                    synthInstrument.setAttackTime((float)adsrSlider[0].getValue() / 100f);
                }
            }
        });

        adsrSlider[1].addChangeListener(new ChangeListener() {
            // This method is called whenever the slider's value is changed

            public void stateChanged(ChangeEvent evt) {

                if (!adsrSlider[1].getValueIsAdjusting()) {
                    // Get new value
                    int value = adsrSlider[1].getValue();
                    System.out.println("2 " + adsrSlider[1].getValue());
                    synthInstrument.setDecayTime((float)adsrSlider[1].getValue() / 100f);
                }
            }
        });

        adsrSlider[2].addChangeListener(new ChangeListener() {
            // This method is called whenever the slider's value is changed

            public void stateChanged(ChangeEvent evt) {

                if (!adsrSlider[2].getValueIsAdjusting()) {
                    // Get new value
                    int value = adsrSlider[2].getValue();
                    System.out.println("3 " + adsrSlider[2].getValue());
                    synthInstrument.setSustainLevel((float)adsrSlider[2].getValue() / 130f);
                }
            }
        });
        adsrSlider[3].addChangeListener(new ChangeListener() {
            // This method is called whenever the slider's value is changed

            public void stateChanged(ChangeEvent evt) {

                if (!adsrSlider[3].getValueIsAdjusting()) {
                    // Get new value
                    int value = adsrSlider[3].getValue();
                    System.out.println("4 " + adsrSlider[3].getValue());
                    synthInstrument.setReleaseRate((float)adsrSlider[2].getValue() / 40f);
                }
            }
        });
    
        adsr.setBorder(BorderFactory.createTitledBorder(
                "ADSR"));
        //adsr.setBackground(Color.gray);
        return adsr;
    }

    private JPanel createVolumePanel() {
        //Volume Slider

        volumeSlider = new JSlider(JSlider.HORIZONTAL, ADSR_MIN, ADSR_MAX, ADSR_INIT);
        volumeSlider.setMajorTickSpacing(20);
        volumeSlider.setMinorTickSpacing(5);
        volumeSlider.setPaintTicks(true);
        JPanel volume = new JPanel();
        volume.add(volumeSlider);
     
        volumeSlider.addChangeListener(new ChangeListener() {
            // This method is called whenever the slider's value is changed

            public void stateChanged(ChangeEvent evt) {

                if (!volumeSlider.getValueIsAdjusting()) {
                    // Get new value
                    int value = volumeSlider.getValue();
                    System.out.println("V " + volumeSlider.getValue());
                    
                    synthInstrument.setVolume((float)volumeSlider.getValue() / 100f);
                }
            }
        });
                
        volume.setBorder(BorderFactory.createTitledBorder(
                "Volume"));
        //volume.setBackground(Color.gray);
        return volume;
    }

    public void actionPerformed(ActionEvent e) {
    }
    

    /**
     * Create the GUI and show it.
     */
    public static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("Synth IAL10");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        JComponent newContentPane = new SynthGui();
        frame.setContentPane(newContentPane);
        frame.setResizable(false);
        frame.setSize(WIDTH, HEIGHT);
        frame.setVisible(true);
    }
}
